package com.example.manage_c20086.api;

import com.example.manage_c20086.cutil.cTool;
import com.pojo.Entity.Game;
import com.pojo.Entity.VenueRentalForm;
import com.pojo.util.Result;
import com.pojo.util.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/game")
public class gameApi {
    @Autowired
    private RestTemplate restTemplate;

    private static final String REST_URL_PREFIX="http://GAME-MANAGEMENT/game";
    private static final String REST_URL_PREFIX_TEMP="http://SITE-MANAGEMENT/site";
    private static final String IDENTIFYING="identifying";
    @PostMapping("/insert")
    public Result<?>insert(Game game, HttpServletRequest httpServletRequest){
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        map.add("vDate", Tool.getDateToStringSort(game.getGDate()));
        map.add("vBegintime",Tool.getDateToStringHours(game.getGBtime()));
        map.add("vEndtime",Tool.getDateToStringHours(game.getGEtime()));
        map.add("sID",game.getGLocation());
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        Result<VenueRentalForm> result1=restTemplate.postForObject(REST_URL_PREFIX_TEMP+"/cpresite",request,Result.class);
        if(result1!=null&&result1.getData()!=null){
            headers = new HttpHeaders();
            map= new LinkedMultiValueMap<>();
            cTool.jsonToMap(game,map);
            map.remove("gDate");
            map.remove("gBtime");
            map.remove("gEtime");
            map.add("gDate", Tool.getDateToStringSort(game.getGDate()));
            map.add("gBtime",Tool.getDateToStringHours(game.getGBtime()));
            map.add("gEtime",Tool.getDateToStringHours(game.getGEtime()));
            headers.setContentType(MediaType.MULTIPART_FORM_DATA);
            headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
            request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
            Result result2=restTemplate.postForObject(REST_URL_PREFIX+"/insert",request,Result.class);
            System.out.println(result2.getData());
            if(result2!=null&&result2.getStatus()!=200){
                headers = new HttpHeaders();
                map= new LinkedMultiValueMap<>();
                map.add("vID",result1.getData().getVID());
                map.add("vPaymentStatus",3);
                headers.setContentType(MediaType.MULTIPART_FORM_DATA);
                headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
                request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
                restTemplate.postForObject(REST_URL_PREFIX+"/payment",request,Result.class);
            }
            return result2;
        }
        return Result.failure();
    }
    @GetMapping("/getlgame")
    public Result<?>getlgame(Integer page,Integer number, HttpServletRequest httpServletRequest){
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        map.add("Page",page);
        map.add("Number",number);
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        return restTemplate.postForObject(REST_URL_PREFIX+"/getlgame", request,Result.class);
    }
    @GetMapping("/getgame")
    public Result<?>getgame(Integer gID, HttpServletRequest httpServletRequest){
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        map.add("gID",gID);
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        return restTemplate.postForObject(REST_URL_PREFIX+"/getgame", request,Result.class);
    }
}
